﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using V5_Common;
using V5_Common.FrameWork;

namespace V5_Data {
    public class V5ArticleHelper : IV5Helper {
        private static readonly V5ArticleHelper m_Instance = new V5ArticleHelper();
        /// <summary>
        /// 
        /// </summary>
        public static V5ArticleHelper Instance {
            get { return m_Instance; }
        }

        #region 变量
        private string _Pager = string.Empty;
        /// <summary>
        /// 分页数据
        /// </summary>
        public string Pager {
            get { return _Pager; }
            set { _Pager = value; }
        }

        private string _PrevUrl = string.Empty;
        /// <summary>
        /// 上一个地址
        /// </summary>
        public string PrevUrl {
            get {
                if (string.IsNullOrEmpty(_PrevUrl)) {
                    _PrevUrl = "javascript:void(0);";
                }
                return _PrevUrl;
            }
            set { _PrevUrl = value; }
        }
        private string _PrevTitle = string.Empty;
        /// <summary>
        /// 上一个标题
        /// </summary>
        public string PrevTitle {
            get {
                if (string.IsNullOrEmpty(_PrevTitle)) {
                    _PrevTitle = "没有了";
                }
                return _PrevTitle;
            }
            set { _PrevTitle = value; }
        }
        private string _NextUrl = string.Empty;
        /// <summary>
        /// 下一个地址
        /// </summary>
        public string NextUrl {
            get {
                if (string.IsNullOrEmpty(_NextUrl)) {
                    _NextUrl = "javascript:void(0);";
                }
                return _NextUrl;
            }
            set { _NextUrl = value; }
        }
        private string _NextTitle = string.Empty;
        /// <summary>
        /// 下一个标题
        /// </summary>
        public string NextTitle {
            get {
                if (string.IsNullOrEmpty(_NextTitle)) {
                    _NextTitle = "没有了";
                }
                return _NextTitle;
            }
            set { _NextTitle = value; }
        }
        private int _PageSize = 20;
        /// <summary>
        /// 分页大小
        /// </summary>
        public int PageSize {
            get { return _PageSize; }
            set { _PageSize = value; }
        }

        private string _ChannelType = "Article";
        /// <summary>
        /// 频道类型
        /// </summary>
        public string ChannelType {
            get { return _ChannelType; }
        }
        #endregion

        #region 模板
        private string _TemplateIndex = "Article/Index";
        /// <summary>
        /// 首页模板
        /// </summary>
        public string TemplateIndex {
            get { return _TemplateIndex; }
        }
        private string _TemplateList = "Article/List";
        /// <summary>
        /// 列表模板
        /// </summary>
        public string TemplateList {
            get { return _TemplateList; }
        }
        private string _TemplateView = "Article/View";
        /// <summary>
        /// 内容模板
        /// </summary>
        public string TemplateView {
            get { return _TemplateView; }
        }
        private string _TemplateSearch = "Article/Search";
        /// <summary>
        /// 搜索列表模板
        /// </summary>
        public string TemplateSearch {
            get { return _TemplateSearch; }
        }
        #endregion

        #region 文章操作
        /// <summary>
        /// 获取文章内容
        /// </summary>
        /// <param name="id">文章ID</param>
        /// <returns></returns>
        public DataRow GetDataRow(object oID) {
            string id = InputHelper.SetString(oID);
            DataTable dt = V5.SQL.Query_By("*", "Tbl_Article", "ID=" + id, "", 1);
            if (dt.Rows.Count > 0) {
                return dt.Rows[0];
            }
            else {
                return dt.NewRow();
            }
        }
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="ClassID"></param>
        /// <param name="isSub"></param>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetList(int ClassID, bool isSub, string what, string where, string orderby, int limit) {
            DataRowCollection drc = null;
            string ClassIDList = ClassID == 0 ? string.Empty : ClassID.ToString() + ",";
            if (isSub) {
                foreach (DataRow dr in GetClassList("ClassID", "ParentID=" + ClassID, string.Empty, 0)) {
                    ClassIDList += dr["ClassID"].ToString() + ",";
                }
            }
            if (ClassIDList.Length > 0) {
                ClassIDList = ClassIDList.Remove(ClassIDList.Length - 1);
            }

            if (!string.IsNullOrEmpty(where)) {
                where += " And ClassID in(" + ClassIDList + ") ";
            }
            else {
                where += " ClassID in(" + ClassIDList + ") ";
            }
            drc = V5.SQL.Query_By(what, "Tbl_Article", where, orderby, limit).Rows;
            return drc;
        }
        #endregion

        #region 分类
        /// <summary>
        /// 获取分类单个属性
        /// </summary>
        /// <param name="oClassID"></param>
        /// <returns></returns>
        public DataRow GetClassSingle(object oClassID) {
            return V5.SQL.Query_By("*", "Tbl_ArticleClass", " ClassID=" + oClassID, "", 1).Rows[0];
        }
        /// <summary>
        /// 获取分类信息
        /// </summary>
        /// <param name="what"></param>
        /// <param name="where"></param>
        /// <param name="orderby"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public DataRowCollection GetClassList(string what, string where, string orderby, int limit) {
            return V5.SQL.Query_By(what, "Tbl_ArticleClass", where, orderby, limit).Rows;
        }
        #endregion

        #region
        public DataTable Query_By(string what, string where, string orderby, int limit) {
            DataTable dt = V5.SQL.Query_By(what, "(Select B.ClassTitle,A.* From Tbl_Article A Left Join Tbl_ArticleClass B On A.ClassId=B.ClassId) AA", where, orderby, limit);
            DataColumn dc = new DataColumn("Url");
            dt.Columns.Add(dc);
            foreach (DataRow item in dt.Rows) {
                item["Url"] = this.UrlView(new { ID = item["ID"].ToString(), PageID = "1" });
            }
            return dt;
        }

        public DataTable Query_By_List(string what, string where, string orderby, int limit) {
            DataTable dt = V5.SQL.Query_By(what, "Tbl_ArticleClass", where, orderby, limit);
            DataColumn dc = new DataColumn("Url");
            dt.Columns.Add(dc);
            foreach (DataRow item in dt.Rows) {
                item["Url"] = this.UrlList(new { ClassID = item["ClassID"].ToString(), PageID = "1" });
            }
            return dt;
        }

        public DataTable Query_By_Pager(string what, string where, string orderby, int limit, int pageIndex, string UrlList) {
            int RecordCount = 0;
            DataTable dt = V5.SQL.Query_Pager(what, "Tbl_Article", where, orderby, limit, pageIndex, ref RecordCount);
            DataColumn dc = new DataColumn("Url");
            dt.Columns.Add(dc);
            foreach (DataRow item in dt.Rows) {
                item["Url"] = this.UrlList(new { ClassID = item["ID"].ToString(), PageID = "1" });
            }
            return dt;
        }

        public DataRow Get_By(string ID) {
            DataTable dt = V5.SQL.Query_By("*", "Tbl_Article", "ID=" + ID, "", 1);
            return dt.Rows[0];
        }


        public DataRow Get_By_List(string ClassID) {
            DataTable dt = V5.SQL.Query_By("*", "Tbl_ArticleClass", "ClassID=" + ClassID, "", 1);
            return dt.Rows[0];
        }

        public string UrlIndex() {
            return V5.Url.Action(this.TemplateIndex, null);
        }

        public string UrlIndex(int showType) {
            return V5.Url.Action(this.TemplateIndex, null, showType);
        }

        public string UrlView(object attributes) {
            return V5.Url.Action(this.TemplateView, attributes);
        }

        public string UrlList(object attributes) {
            return V5.Url.Action(this.TemplateList, attributes);
        }

        public string UrlView(object attributes, int showType) {
            return V5.Url.Action(this.TemplateView, attributes, showType);
        }

        public string UrlList(object attributes, int showType) {
            return V5.Url.Action(this.TemplateList, attributes, showType);
        }
        public string UrlSearch(object attributes) {
            return V5.Url.Action(this.TemplateList, attributes);
        }

        public string UrlSearch(object attributes, int showType) {
            return V5.Url.Action(this.TemplateList, attributes, showType);
        }
        #endregion
    }
}